package com.example.service.impl.carecommunity.daycare;

import com.example.bean.carecommunity.SheQujujia;
import com.example.bean.carecommunity.Vo.RiZhaoVO;
import com.example.bean.configuration.zhibiaosystem.XzLiebiao;
import com.example.bean.configuration.zhibiaosystem.Zhibiao;
import com.example.bean.institutioncare.institutionadmin.Basicdata;
import com.example.mapper.carecommunity.daycare.RijianzhaoliaoMapper;
import com.example.service.carecommunity.daycare.RijianzhaoliaoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RijianzhaoliaoServiceImpl implements RijianzhaoliaoService {
    @Autowired
    RijianzhaoliaoMapper mapper;

    @Override
    public PageInfo<SheQujujia> selectTijianzhaoliaoAll(RiZhaoVO riZhaoVO) {
        System.out.println("接收到的值为："+riZhaoVO);
        Integer cp = riZhaoVO.getCurrentPage()==null?1:riZhaoVO.getCurrentPage();
        Integer ps = riZhaoVO.getPageSize()==null?10:riZhaoVO.getPageSize();
        PageHelper.startPage(cp,ps);
        PageInfo<SheQujujia> pageinfo = new PageInfo<SheQujujia>(mapper.selectTijianzhaoliaoAll(riZhaoVO));
        return pageinfo;
    }

    @Override
    public Zhibiao getzhibiao(String leixiangming) {
        return mapper.getzhibiao(leixiangming);
    }

    @Override
    public List<XzLiebiao> getxiangzhi(String noid) {
        return mapper.getxiangzhi(noid);
    }

    @Override
    public List<Basicdata> getbasiAll() {
        return mapper.getbasiAll();
    }

    @Override
    public Integer addrizhao(SheQujujia rijianzhaoliao) {
        //根据机构id查询出这个机构的特困人员供养机构类型和机构性质的信息后添加到主表中
        SheQujujia rz = mapper.selectJiGou(rijianzhaoliao.getJigouId());
        System.err.println("查到的机构信息："+rz);
        //特困人员供养机构类型
        rijianzhaoliao.setExtremelyNeedySupportType(rz.getExtremelyNeedySupportType());
        //机构性质
        rijianzhaoliao.setInstitutionsNature(rz.getInstitutionsNature());
        //往数据库中进行添加操作
        System.err.println("添加日间照料中心的信息："+rijianzhaoliao);
        Integer a = mapper.addrizhao(rijianzhaoliao);
        //原因判断
        String xuke = rijianzhaoliao.getXuke();
        System.err.println("许可信息："+xuke);
        System.out.println("原因信息："+rijianzhaoliao.getYuanyin());
        //获取到刚添加的这条数据的id信息
        String noid = rijianzhaoliao.getNoid();
        System.err.println("id信息："+noid);
        //如果有许可证，则原因不用显示，填0即可,默认也不让其显示
        mapper.updateXukeBasc(noid);
        if(xuke.equals("0")){//没有许可证，显示原因，进行添加
            for(String i : rijianzhaoliao.getYuanyin()){//获取到没有许可证的原因
                if(i.equals("0")){
                    mapper.updatea(noid);
                }
                if(i.equals("1")){
                    mapper.updateb(noid);
                }
                if(i.equals("2")){
                    mapper.updatec(noid);
                }
                if(i.equals("3")){
                    mapper.updated(noid);
                }
            }
        }
        String fuwuxiangid = rijianzhaoliao.getFuwuxiangid();
        System.err.println("服务项的id："+fuwuxiangid);
        System.err.println(""+rijianzhaoliao.getFwgnlist());
        //往中间表中添加服务信息
        for(String index:rijianzhaoliao.getFwgnlist()) {
            mapper.addzhongjian(index, fuwuxiangid, noid);
        }
        return a;
    }

    @Override
    public Integer editRizhaoguanli(List<String> ids) {
        Integer i = null;
        for (String index : ids) {
            i = mapper.updateStutaDelete(index);
        }
        return i;
    }

    @Override
    public SheQujujia lookrizhao(String noid) {
        //先将主页面信息进行回显
        SheQujujia rizhaozx = mapper.lookrizhao(noid);
        System.out.println("查到的回显信息："+rizhaozx);
        //没有许可证=0
        if(rizhaozx.getXuke().equals("0")){
            System.out.println("进入a");
            //0代表选择了-无相关消防审核意见
            if(rizhaozx.getIsXiaofangyijian()==0){
                System.out.println("进入1");
                rizhaozx.getYuanyin().add("0");//往集合中存0，从第0个开始
            }
            if(rizhaozx.getIsShiyaojianyijian()==0){
                System.out.println("进入2");
                rizhaozx.getYuanyin().add("1");
            }
            if(rizhaozx.getIsHuanpingyijian()==0){
                System.out.println("进入3");
                rizhaozx.getYuanyin().add("2");
            }
            if(rizhaozx.getIsOther()==0){
                System.out.println("进入4");
                rizhaozx.getYuanyin().add("3");
            }
        }
        rizhaozx.setFwgnlist(mapper.lookXiangzhi(noid));
        rizhaozx.setFuwuxiangid(mapper.lookFuwuxiangid(noid));
        return rizhaozx;
    }

    @Override
    public Zhibiao getxiangzhilist(String noid) {
        return mapper.getxiangzhilist(noid);
    }

    @Override
    public Integer updaterizhao(SheQujujia rijianzhaoliao) {
        System.out.println("前台修改传来的信息为："+rijianzhaoliao);
        //先将主表信息进行修改
        Integer a = mapper.updatezhubiao(rijianzhaoliao);
        //修改服务内容信息
        //1.先根据日间照料中心id将中间表中的服务信息进行删除
        mapper.deletezhogjian(rijianzhaoliao.getNoid());
        //2.往中间表中传入新的信息
        for(String index:rijianzhaoliao.getFwgnlist()) {
            mapper.addzhongjian(index, rijianzhaoliao.getFuwuxiangid(), rijianzhaoliao.getNoid());
        }
        //许可证信息修改
        //有了许可证将许可证信息正常存储，原因信息改为不显示
        mapper.updateXukeBasc(rijianzhaoliao.getNoid());
        //没有许可证，根据前台传来的原因信息，被选中的值进行显示，没被选中的不显示，可以先让原因全部改为不显示
        if(rijianzhaoliao.getXuke().equals("0")){
            for (String index : rijianzhaoliao.getYuanyin()) {
                if(index.equals("0")){
                    mapper.updatea(rijianzhaoliao.getNoid());
                }
                if(index.equals("1")){
                    mapper.updateb(rijianzhaoliao.getNoid());
                }
                if (index.equals("2")){
                    mapper.updatec(rijianzhaoliao.getNoid());
                }
                if (index.equals("3")){
                    mapper.updatec(rijianzhaoliao.getNoid());
                }
            }
        }
        return a;
    }
}